

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.DTO.ComponentsDTO;
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.Loging;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.DTO;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Utility.Extensions;
using Essensoft.Paylink.Alipay.Domain;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Crypto;
using SqlSugar;
using static NPOI.HSSF.Util.HSSFColor;


namespace CoreCms.Net.Services
{
    /// <summary>
    /// 单页 接口实现
    /// </summary>
    public class CoreCmsPagesServices : BaseServices<CoreCmsPages>, ICoreCmsPagesServices
    {
        private readonly ICoreCmsPagesRepository _dal;
        private readonly ICoreCmsPagesItemsRepository _pagesItemsRepository;
        private readonly ICoreCmsPromotionServices _promotionServices;
        private readonly ICoreCmsNoticeServices _noticeServices;
        private readonly ICoreCmsGoodsCategoryServices _goodsCategoryServices;
        private readonly ICoreCmsSettingServices _settingServices;
        private readonly ICoreCmsGoodsServices _goodsServices;
        private readonly ICoreCmsArticleServices _articleServices;
        private readonly ICoreCmsArticleTypeServices _articleTypeServices;
        private readonly ICoreCmsPromotionConditionServices _promotionConditionServices;
        private readonly ICoreCmsPinTuanRuleServices _pinTuanRuleServices;
        private readonly ICoreCmsServicesServices _servicesServices;
        private readonly ICoreCmsGoodsCategoryExtendServices _goodsCategoryExtendServices;


        private readonly IUnitOfWork _unitOfWork;
        public CoreCmsPagesServices(IUnitOfWork unitOfWork
            , ICoreCmsPagesRepository dal
            , ICoreCmsPagesItemsRepository pagesItemsRepository, ICoreCmsPromotionServices promotionServices, ICoreCmsNoticeServices noticeServices, ICoreCmsGoodsCategoryServices goodsCategoryServices, ICoreCmsSettingServices settingServices, ICoreCmsGoodsServices goodsServices, ICoreCmsArticleServices articleServices, ICoreCmsPromotionConditionServices promotionConditionServices, ICoreCmsPinTuanRuleServices pinTuanRuleServices, ICoreCmsServicesServices servicesServices, ICoreCmsArticleTypeServices articleTypeServices, ICoreCmsGoodsCategoryExtendServices goodsCategoryExtendServices)
        {
            this._dal = dal;
            _pagesItemsRepository = pagesItemsRepository;
            _promotionServices = promotionServices;
            _noticeServices = noticeServices;
            _goodsCategoryServices = goodsCategoryServices;
            _settingServices = settingServices;
            _goodsServices = goodsServices;
            _articleServices = articleServices;
            _promotionConditionServices = promotionConditionServices;
            _pinTuanRuleServices = pinTuanRuleServices;
            _servicesServices = servicesServices;
            _articleTypeServices = articleTypeServices;
            _goodsCategoryExtendServices = goodsCategoryExtendServices;
            base.BaseDal = dal;
            _unitOfWork = unitOfWork;
        }


        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> InsertAsync(CoreCmsPages entity)
        {
            return await _dal.InsertAsync(entity);
        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> UpdateAsync(CoreCmsPages entity)
        {
            return await _dal.UpdateAsync(entity);
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> DeleteByIdAsync(int id)
        {
            return await _dal.DeleteByIdAsync(id);
        }

        /// <summary>
        /// 更新设计
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> UpdateDesignAsync(FmPagesUpdate entity)
        {
            return await _dal.UpdateDesignAsync(entity);
        }


        /// <summary>
        /// 复制一个同样的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> CopyByIdAsync(int id)
        {
            return await _dal.CopyByIdAsync(id);
        }


        #region 获取首页数据
        /// <summary>
        /// 获取首页数据
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<WebApiCallBack> GetPageConfig(string code)
        {

            var jm = new WebApiCallBack();

            var wherePage = PredicateBuilder.True<CoreCmsPages>();

            wherePage = code == "mobile_home" ? wherePage.And(p => p.type == 1) : wherePage.And(p => p.code == code);

            var model = await _dal.QueryByClauseAsync(wherePage, true, true);
            if (model == null)
            {
                return jm;
            }
            jm.status = true;
            var items = await _pagesItemsRepository.QueryListByClauseAsync(p => p.pageCode == model.code, p => p.sort, OrderByType.Asc, true, true);

            var itemsDto = new List<PagesItemsDto>();
            foreach (var item in items)
            {
                var dto = new PagesItemsDto();
                dto.id = item.id;
                dto.widgetCode = item.widgetCode;
                dto.pageCode = item.pageCode;
                dto.positionId = item.positionId;
                dto.sort = item.sort;

                item.parameters = item.parameters.Replace("/images/empty-banner.png", "/static/images/common/empty-banner.png");

                switch (item.widgetCode)
                {
                    //搜索
                    case "search":
                    case "tabBar":
                        dto.parameters = JsonConvert.DeserializeObject(item.parameters);
                        break;
                    case "notice":
                        {
                            var parameters = (JObject)JsonConvert.DeserializeObject(item.parameters);
                            if (parameters != null && parameters.ContainsKey("type") && parameters["type"].ToString() == "auto")
                            {
                                var list = await _noticeServices.QueryComponentAsync(p => p.isDel == false);
                                if (list != null && list.Any())
                                {
                                    var result = JArray.FromObject(list);
                                    parameters.Remove("list");
                                    parameters.Add("list", result);
                                }
                            }
                            else if (parameters != null && parameters.ContainsKey("type") && parameters["type"].ToString() == "choose")
                            {

                                var where = PredicateBuilder.True<CoreCmsNotice>();

                                var orderBy = string.Empty;
                                var noticeIdsStr = string.Empty;
                                if (parameters != null && parameters.ContainsKey("list"))
                                {
                                    var result = JArray.Parse(parameters["list"].ToString());
                                    var noticeIds = result.Select(ss => ((JObject)ss)["id"].ObjectToInt(0)).Where(noticeId => noticeId > 0).ToList();
                                    where = where.And(p => noticeIds.Contains(p.id));
                                    //if (noticeIds.Any())
                                    //{
                                    //    noticeIdsStr = string.Join(",", noticeIdsStr);
                                    //    //按照固定的序列id进行排序
                                    //    if (AppSettingsConstVars.DbDbType == DbType.SqlServer.ToString())
                                    //    {
                                    //        orderBy = " CHARINDEX(RTRIM(CAST(id as NCHAR)),'" + noticeIdsStr + "') ";
                                    //    }
                                    //    else if (AppSettingsConstVars.DbDbType == DbType.MySql.ToString())
                                    //    {
                                    //        orderBy = " find_in_set(id,'" + noticeIdsStr + "') ";
                                    //    }
                                    //}
                                }
                                var notices = await _noticeServices.QueryListByClauseAsync(where, p => p.createTime, OrderByType.Desc);
                                if (notices != null && notices.Any())
                                {
                                    var result = JArray.FromObject(notices);
                                    parameters.Remove("list");
                                    parameters.Add("list", result);
                                }
                                else
                                {
                                    parameters.Remove("list");
                                    parameters.Add("list", new JArray());
                                }
                            }

                            dto.parameters = parameters;
                            break;
                        }
                    case "imgSlide":
                        dto.parameters = JsonConvert.DeserializeObject(item.parameters);
                        break;
                    case "coupon":
                        {
                            var parameters = (JObject)JsonConvert.DeserializeObject(item.parameters);
                            if (parameters != null && parameters.ContainsKey("limit") && parameters["limit"].ObjectToInt(0) != 0)
                            {
                                var list = await _promotionServices.ReceiveCouponList(parameters["limit"].ObjectToInt(0));
                                if (list != null && list.Any())
                                {
                                    var result = JArray.FromObject(list);
                                    parameters.Remove("list");
                                    parameters.Add("list", result);
                                }
                            }

                            dto.parameters = parameters;
                            break;
                        }
                    case "blank":
                        dto.parameters = JsonConvert.DeserializeObject(item.parameters);
                        break;
                    case "textarea":
                        {
                            var parameters = new JObject();
                            parameters["value"] = item.parameters;
                            dto.parameters = parameters;
                            break;
                        }
                    case "video":
                    case "imgWindow":
                    case "imgSingle":
                        dto.parameters = JsonConvert.DeserializeObject(item.parameters);
                        break;
                    case "goodTabBar":
                        {
                            var parameters = (JObject)JsonConvert.DeserializeObject(item.parameters);
                            if (parameters != null && parameters.ContainsKey("list"))
                            {
                                var list = JArray.Parse(parameters["list"].ToString());
                                var newList = new JArray();
                                foreach (var jToken in list)
                                {
                                    var child = (JObject)jToken;
                                    var where = PredicateBuilder.True<GoodListDTO>();
                                    if (child != null && child.ContainsKey("type") && child["type"].ToString() == "auto")
                                    {
                                        //商品分类,同时取所有子分类
                                        if (child.ContainsKey("classifyId") && child["classifyId"].ObjectToInt(0) > 0)
                                        {
                                            var classifyId = child["classifyId"].ObjectToInt(0);
                                            var gc = await _goodsCategoryServices.GetCaChe();
                                            var childCats = gc.Where(p => p.parentId == classifyId).ToList();
                                            var catIds = childCats != null && childCats.Any()
                                                ? childCats.Select(p => p.id).ToList()
                                                : new List<int>();
                                            catIds.Add(classifyId);

                                            where = where.And(p => catIds.Contains(p.goodsCategoryId));
                                            //扩展分类 CoreCmsGoodsCategory
                                        }
                                        //品牌筛选
                                        if (child.ContainsKey("brandId") && child["brandId"].ObjectToInt(0) > 0)
                                        {
                                            var brandId = child["brandId"].ObjectToInt(0);
                                            where = where.And(p => p.brandId == brandId);
                                        }

                                        var limit = 0;
                                        if (child.ContainsKey("limit") && child["limit"].ObjectToInt(0) > 0)
                                        {
                                            limit = child["limit"].ObjectToInt(0);
                                        }
                                        limit = limit > 0 ? limit : 10;

                                        var goods = await _goodsServices.QueryListByComponentsAsync(where, limit, p => p.createTime, OrderByType.Desc, true, true, 60);

                                        if (goods != null && goods.Any())
                                        {
                                            var result = JArray.FromObject(goods);
                                            child.Remove("list");
                                            child.Add("list", result);
                                        }
                                        else
                                        {
                                            child.Remove("list");
                                            child.Add("list", new JArray());
                                        }
                                    }
                                    else
                                    {
                                        var orderBy = string.Empty;
                                        string goodidsStr;
                                        var count = 0;
                                        if (child != null && child.ContainsKey("list"))
                                        {
                                            var result = JArray.Parse(child["list"].ToString());
                                            var goodids = result.Select(ss => ((JObject)ss)["id"].ObjectToInt(0)).Where(goodid => goodid > 0).ToList();
                                            where = where.And(p => goodids.Contains(p.id));
                                            if (goodids.Any())
                                            {
                                                goodidsStr = string.Join(",", goodids);
                                                //按照id序列打乱后的顺序排序
                                                if (AppSettingsConstVars.DbDbType == DbType.SqlServer.ToString())
                                                {
                                                    orderBy = " CHARINDEX(RTRIM(CAST(id as NCHAR)),'" + goodidsStr + "') ";
                                                }
                                                else if (AppSettingsConstVars.DbDbType == DbType.MySql.ToString())
                                                {
                                                    orderBy = " find_in_set(id,'" + goodidsStr + "') ";
                                                }

                                                count = goodids.Count;
                                            }
                                        }
                                        var goods = await _goodsServices.QueryListByComponentsAsync(where, count, orderBy, true, true, 60);
                                        if (goods != null && goods.Any())
                                        {
                                            var result = JArray.FromObject(goods);
                                            child.Remove("list");
                                            child.Add("list", result);
                                        }
                                        else
                                        {
                                            child.Remove("list");
                                            child.Add("list", new JArray());
                                        }
                                    }
                                    newList.Add(child);
                                }
                                if (newList != null && newList.Any())
                                {
                                    parameters.Remove("list");
                                    parameters.Add("list", newList);
                                }
                            }
                            dto.parameters = parameters;
                            break;
                        }
                    case "goods":
                        {
                            var parameters = (JObject)JsonConvert.DeserializeObject(item.parameters);
                            var where = PredicateBuilder.True<GoodListDTO>();
                            if (parameters != null && parameters.ContainsKey("type") && parameters["type"].ToString() == "auto")
                            {
                                //商品分类,同时取所有子分类
                                if (parameters.ContainsKey("classifyId") && parameters["classifyId"].ObjectToInt(0) > 0)
                                {
                                    var classifyId = parameters["classifyId"].ObjectToInt(0);
                                    var gc = await _goodsCategoryServices.GetCaChe();
                                    var childCats = gc.Where(p => p.parentId == classifyId).ToList();
                                    var catIds = childCats != null && childCats.Any()
                                        ? childCats.Select(p => p.id).ToList()
                                        : new List<int>();
                                    catIds.Add(classifyId);

                                    //where = where.And(p => catIds.Contains(p.goodsCategoryId));


                                    //扩展分类
                                    var extends = await _goodsCategoryExtendServices.QueryListByClauseAsync(p => p.goodsCategroyId == classifyId);
                                    if (extends.Any())
                                    {
                                        var extGoodIds = extends.Select(p => p.goodsId).ToList();
                                        where = where.And(p => catIds.Contains(p.goodsCategoryId) || extGoodIds.Contains(p.id));
                                    }
                                    else
                                    {
                                        where = where.And(p => catIds.Contains(p.goodsCategoryId));
                                    }


                                    //扩展分类 CoreCmsGoodsCategory
                                }
                                //品牌筛选
                                if (parameters.ContainsKey("brandId") && parameters["brandId"].ObjectToInt(0) > 0)
                                {
                                    var brandId = parameters["brandId"].ObjectToInt(0);
                                    where = where.And(p => p.brandId == brandId);
                                }
                                //数量
                                var limit = 0;
                                if (parameters.ContainsKey("limit") && parameters["limit"].ObjectToInt(0) > 0)
                                {
                                    limit = parameters["limit"].ObjectToInt(0);
                                }
                                limit = limit > 0 ? limit : 10;

                                var goods = await _goodsServices.QueryListByComponentsAsync(where, limit, " sort desc,id desc ", true, true, 60);
                                if (goods != null && goods.Any())
                                {
                                    var result = JArray.FromObject(goods);
                                    parameters.Remove("list");
                                    parameters.Add("list", result);
                                }
                                else
                                {
                                    parameters.Remove("list");
                                    parameters.Add("list", new JArray());
                                }
                            }
                            else
                            {
                                var orderBy = string.Empty;
                                string goodidsStr;
                                var count = 0;
                                if (parameters != null && parameters.ContainsKey("list"))
                                {
                                    var result = JArray.Parse(parameters["list"].ToString());
                                    var goodids = new List<int>();
                                    foreach (var ss in result)  //查找某个字段与值
                                    {
                                        var goodid = ((JObject)ss)["id"].ObjectToInt(0);
                                        if (goodid > 0)
                                        {
                                            goodids.Add(goodid);
                                        }
                                    }
                                    where = where.And(p => goodids.Contains(p.id));
                                    if (goodids.Any())
                                    {
                                        goodidsStr = string.Join(",", goodids);
                                        //按照id序列打乱后的顺序排序
                                        if (AppSettingsConstVars.DbDbType == DbType.SqlServer.ToString())
                                        {
                                            orderBy = " CHARINDEX(RTRIM(CAST(id as NCHAR)),'" + goodidsStr + "') ";
                                        }
                                        else if (AppSettingsConstVars.DbDbType == DbType.MySql.ToString())
                                        {
                                            orderBy = " find_in_set(id,'" + goodidsStr + "') ";
                                        }
                                    }
                                    count = goodids.Count;
                                }
                                var goods = await _goodsServices.QueryListByComponentsAsync(where, count, orderBy, true, true, 60);
                                if (goods != null && goods.Any())
                                {
                                    var result = JArray.FromObject(goods);
                                    parameters.Remove("list");
                                    parameters.Add("list", result);
                                }
                                else
                                {
                                    parameters.Remove("list");
                                    parameters.Add("list", new JArray());
                                }
                            }

                            dto.parameters = parameters;
                            break;
                        }
                    case "article":
                        dto.parameters = JsonConvert.DeserializeObject(item.parameters);
                        break;
                    case "articleClassify":
                        {
                            var parameters = (JObject)JsonConvert.DeserializeObject(item.parameters);
                            if (parameters != null)
                            {
                                if (parameters.ContainsKey("articleClassifyId") && parameters["articleClassifyId"].ObjectToInt(0) > 0)
                                {
                                    var articleClassifyId = parameters["articleClassifyId"].ObjectToInt(0);
                                    var limit = parameters["limit"].ObjectToInt(0);
                                    limit = limit > 0 ? limit : 20;
                                    var articleClassifyObj = await _articleTypeServices.QueryByClauseAsync(p => p.id == articleClassifyId, true, true);
                                    var articleClassifyObjJson = JsonConvert.SerializeObject(articleClassifyObj);

                                    parameters.Add("articleClassifyObj", JToken.Parse(articleClassifyObjJson));

                                    var list = await _articleServices.QueryPageAsync(p => p.typeId == articleClassifyId && p.isPub == true, p => p.createTime, OrderByType.Desc, 1, limit, true, true);
                                    if (list != null && list.Any())
                                    {
                                        var result = JArray.FromObject(list);
                                        parameters.Remove("list");
                                        parameters.Add("list", result);
                                    }
                                }
                            }

                            dto.parameters = parameters;
                            break;
                        }
                    case "navBar":
                        dto.parameters = JsonConvert.DeserializeObject(item.parameters);
                        break;
                    case "groupPurchase":
                        {
                            var parameters = (JObject)JsonConvert.DeserializeObject(item.parameters);
                            if (parameters != null && parameters.ContainsKey("list"))
                            {
                                var result = JArray.Parse(parameters["list"].ToString());
                                var ids = new List<int>();
                                foreach (var jToken in result)
                                {
                                    var ss = (JObject)jToken;
                                    if (ss.ContainsKey("id"))
                                    {
                                        //判断拼团状态
                                        var dt = DateTime.Now;
                                        var promotionId = ss["id"].ObjectToInt(0);
                                        if (promotionId > 0)
                                        {
                                            ids.Add(promotionId);
                                        }
                                    }
                                }

                                if (ids.Any())
                                {
                                    var list = await _promotionServices.GetGroupListForPages(ids.ToArray());
                                    parameters.Remove("list");
                                    parameters.Add("list", JArray.FromObject(list));
                                }
                            }

                            dto.parameters = parameters;
                            break;
                        }
                    case "seckill":
                        {
                            var parameters = (JObject)JsonConvert.DeserializeObject(item.parameters);
                            if (parameters != null && parameters.ContainsKey("list"))
                            {
                                var result = JArray.Parse(parameters["list"].ToString());
                                var ids = new List<int>();
                                foreach (var jToken in result)
                                {
                                    var ss = (JObject)jToken;
                                    if (ss.ContainsKey("id"))
                                    {
                                        //判断拼团状态
                                        var dt = DateTime.Now;
                                        var promotionId = ss["id"].ObjectToInt(0);
                                        if (promotionId > 0)
                                        {
                                            ids.Add(promotionId);
                                        }
                                    }
                                }

                                if (ids.Any())
                                {
                                    var list = await _promotionServices.GetGroupListForPages(ids.ToArray());
                                    parameters.Remove("list");
                                    parameters.Add("list", JArray.FromObject(list));
                                }
                            }
                            dto.parameters = parameters;
                            break;
                        }
                    case "record":
                        dto.parameters = JsonConvert.DeserializeObject(item.parameters);
                        break;
                    case "pinTuan":
                        {
                            var parameters = (JObject)JsonConvert.DeserializeObject(item.parameters);
                            if (parameters != null && parameters.ContainsKey("list"))
                            {
                                var result = JArray.Parse(parameters["list"].ToString());
                                var newReslut = new JArray();

                                var ruleIdIds = (from JObject ss in result where ss.ContainsKey("goodsId") select ss["id"].ObjectToInt(0)).ToArray();

                                if (ruleIdIds.Any())
                                {
                                    var dtoData = await _pinTuanRuleServices.GetPinTuanInfos(ruleIdIds, p => p.sortId, OrderByType.Asc, 1, result.Count);
                                    foreach (var itemDto in dtoData)
                                    {
                                        //判断拼团状态
                                        var dt = DateTime.Now;

                                        if (itemDto.startTime > dt)
                                        {
                                            itemDto.startStatus = (int)GlobalEnumVars.PinTuanRuleStatus.notBegun;
                                            itemDto.timestamp = (int)itemDto.startTime.Subtract(dt).TotalSeconds;
                                        }
                                        else if (itemDto.startTime <= dt && itemDto.endTime > dt)
                                        {
                                            itemDto.startStatus = (int)GlobalEnumVars.PinTuanRuleStatus.begin;
                                            itemDto.timestamp = (int)itemDto.endTime.Subtract(dt).TotalSeconds;
                                        }
                                        else
                                        {
                                            itemDto.startStatus = (int)GlobalEnumVars.PinTuanRuleStatus.haveExpired;
                                        }

                                        itemDto.pinTuanPrice -= itemDto.discountAmount;
                                        if (itemDto.pinTuanPrice < 0) itemDto.pinTuanPrice = 0;

                                        newReslut.Add(JObject.FromObject(itemDto));
                                    }
                                    parameters.Remove("list");
                                    parameters.Add("list", newReslut);
                                }
                            }
                            dto.parameters = parameters;
                            break;
                        }
                    case "service":
                        {
                            var parameters = (JObject)JsonConvert.DeserializeObject(item.parameters);

                            if (parameters != null && parameters.ContainsKey("list"))
                            {
                                var result = JArray.Parse(parameters["list"].ToString());
                                var ids = (from JObject ss in result where ss.ContainsKey("id") select ((JObject)ss)["id"].ObjectToInt(0) into id where id > 0 select id).ToList();

                                if (ids.Any())
                                {
                                    var serviceObj = await _servicesServices.QueryListByClauseAsync(p => ids.Contains(p.id) && p.status == (int)GlobalEnumVars.ServicesStatus.Shelve && p.amount > 0, p => p.id, OrderByType.Asc, true, true);
                                    foreach (var jToken in result)
                                    {
                                        var ss = (JObject)jToken;
                                        var id = ss["id"].ObjectToInt(0);
                                        var serviceInfo = serviceObj.Find(p => p.id == id);
                                        if (serviceInfo != null)
                                        {
                                            ss["title"] = serviceInfo.title;
                                            ss["description"] = serviceInfo.description;
                                            ss["thumbnail"] = serviceInfo.thumbnail;
                                            int openStatus;
                                            int lastTime = 0;
                                            var dt = DateTime.Now;
                                            if (serviceInfo.startTime > dt)
                                            {
                                                openStatus = (int)GlobalEnumVars.ServicesOpenStatus.notBegun;
                                                var ts = serviceInfo.startTime.Subtract(dt);
                                                lastTime = (int)ts.TotalSeconds;
                                            }
                                            else if (serviceInfo.startTime <= dt && serviceInfo.endTime > dt)
                                            {
                                                openStatus = (int)GlobalEnumVars.ServicesOpenStatus.begin;

                                                var ts = serviceInfo.endTime.Subtract(dt);
                                                lastTime = (int)ts.TotalSeconds;
                                            }
                                            else
                                            {
                                                openStatus = (int)GlobalEnumVars.ServicesOpenStatus.haveExpired;
                                            }
                                            ss["openStatus"] = openStatus;
                                            //ss.Add("openStatus", openStatus);
                                            ss.Add("lastTime", lastTime);
                                        }
                                    }
                                }

                                parameters.Remove("list");
                                parameters.Add("list", result);
                            }

                            dto.parameters = parameters;
                            break;
                        }
                    case "adpop":
                    case "topImgSlide":
                        dto.parameters = JsonConvert.DeserializeObject(item.parameters);
                        break;
                    default:
                        dto.parameters = JsonConvert.DeserializeObject(item.parameters);
                        break;
                }
                itemsDto.Add(dto);
            }

            jm.data = new
            {
                model.code,
                desc = model.description,
                model.id,
                model.layout,
                model.name,
                model.type,
                items = itemsDto
            };

            return jm;
        }

        #endregion




    }
}
